require 'time'
load 'enumerable.rb'

class Movie
  include Comparable
  include Enumerable
  
  attr_reader :name
  attr_reader :showtime
  
  def initialize(name,showtime)
    @name = name
	if showtime.is_a? Time
	  @showtime = showtime
	elsif showtime.is_a? String
	  @showtime = Time.parse(showtime)
	else
	  raise ArgumentError, 'Argument is not Time or String'
	end
  end
  
  def name_and_showtime 
	time = @showtime.strftime("%I:%M %p")
	if Time.now > @showtime #Showtime in the past
	  @name + ": already started at " + time
	else
	  @name + ": next show at " + time
	end
  end
  
  def <=> movie
	self.showtime <=> movie.showtime
  end	
end

movies = []
movies << Movie.new("Orphan", Time.parse("1:00 pm"))
movies << Movie.new("Orphan", Time.parse("4:05 pm"))
movies << Movie.new("Orphan", Time.parse("7:00 pm"))
movies << Movie.new("GI Joe", Time.parse("1:05 pm"))
movies << Movie.new("GI Joe", Time.parse("4:30 pm"))
movies << Movie.new("GI Joe", Time.parse("6:50 pm"))
movies << Movie.new("Rocky Horror", Time.parse("12:00 am"))
movies << Movie.new("District 9", Time.parse("11:00 am"))
movies << Movie.new("District 9", Time.parse("1:00 pm"))
movies << Movie.new("GI Joe", Time.parse("11:50 pm"))

#--Higher-order functions & functional programming--

#3,1
movies.select{|x| x.showtime > Time.parse("5:00 pm")}

#3,2
movies.select{|x| x.showtime > Time.parse("5:00 pm")}.length

#3,3
movies.select{|x| x.showtime > Time.parse("5:00 pm")}.map{|x| x.name}.uniq